define(["jquery","bbcare", "scprit/department/vm/assessmentScreening/assessmentScreeningList.ui.js",
    "scprit/department/vm/assessmentScreening/assessmentScreeningList.servers.js",
    "scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.servers.js"
    ],function($,$bb,$ui,$servers,$dataDeal){
    var event ={};
    event.toSearch = function(type,val){
        var searchFn =  function(dt,flag){
            $("#infoLists").addClass("show");
            if(dt.data.rows.length > 0){
                for(var i = 0;i<dt.data.rows.length;i++){
                    dt.data.rows[i].age =  $dataDeal.age($dataDeal.dateDiff($dataDeal.getNowFormatDate(),dt.data.rows[i].birthday),dt.data.rows[i].pregnancyDay);
                }
                $ui.searchListInit({"id":"#infoLists","data":dt.data.rows});
            }else{
                if(flag){
                    $("#infoLists").html("该患者可能还没有建档");
                }
                if(!flag){
                    var key= $.trim($("#searchUser").val());
                    if(key){
                        var params2 = $dataDeal.searchParams(key,'1');
                        $dataDeal.search(params2,searchFn,'1');
                    }
                }
            }
            $("#infoLists").on("click","li",function(){
                var userId = $(this).attr("userId");
                var obj = {
                    "type":type,
                    "userId":userId
                };
                var birth = $(this).attr("birthday");
                var pregnancDay = $(this).attr("pregnancDay");
                var actualDay = $dataDeal.dateDiff(birth,$dataDeal.getNowFormatDate());
                var monthAge = $dataDeal.monthAge(actualDay ,pregnancDay);
                switch(type) {
                    case 'HighriskPregnant':
                        $bb.openPage("highRiskPregnantwomen",obj);
                    break
                    case 'SAS':
                        $bb.openPage("anxiety",obj);
                    break
                    case 'SDS':
                        $bb.openPage("depression",obj);
                    break
                    case 'EPDS':
                        $bb.openPage("postpartumDepression",obj);
                    break
                    case 'AIMS':
                        if( monthAge*1 > 19 ){
                            $bb.toastShow("该宝宝超过19月龄，无法做Alberta婴儿运动评测",3000);
                        }else{
                            $bb.openPage("albertaAssessmentOfInfants",obj);
                        }
                    break
                    case 'PEABODY':
                        $bb.openPage("peabodyScale",obj);
                    break
                    case 'BSID2':
                        if( monthAge*1 > 31 || monthAge*1 < 2 ){
                            $bb.toastShow("贝利婴幼儿发展量表2适用于2~30月龄的宝宝，当前宝宝不满足测试年龄条件",3000);
                        }else {
                            $bb.openPage("bsid2", obj);
                        }
                    break
                    case 'BSID':
                        $bb.openPage("bsid3", obj);
                    break
                    case 'Temperament':
                        if (monthAge*1 > 36) return $bb.toastShow("婴幼儿气质发育评估适用于0~36月龄的宝宝，当前宝宝不满足测试年龄条件",3000);
                        $bb.openPage("temperamentAssessment", obj);
                    break
                    case 'nbna':
                        $bb.openPage("nbnaAssessment", obj);
                    break
                }

            });
        };
        if(val){
            var params = $dataDeal.searchParams(val);
            $dataDeal.search(params,searchFn);
        }

        $('#myModal').on('hidden.bs.modal', function (e) {
            $("#searchUser").val("");
            $("#infoLists").removeClass("show").html("");
        });

        $("#searchUser").on("input propertychange",function(){
            var key= $.trim($(this).val());
            if(key){
                var params = $dataDeal.searchParams(key);
                $dataDeal.search(params,searchFn);
            }else{
                $("#infoLists").removeClass("show");
            }
        });
        $("#toSearch").click(function(){
            var key= $.trim($("#searchUser").val());
            if(key){
                var params = $dataDeal.searchParams(key);
                $dataDeal.search(params,searchFn);
            }else{
                $("#infoLists").removeClass("show");
            }
        });
    };
    return event;
});